package com.kuaimacode.kframework.agent.controller.service;

import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.services.serv.AgentServService;
import com.kuaimacode.kframework.agent.services.service.OneClassService;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.common.JsonResult;
import com.kuaimacode.kframework.constants.PageConstants;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminUser;
import com.kuaimacode.kframework.mybatis.models.service.OneClass;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.service.DictService;
import com.kuaimacode.kframework.util.DictUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/03/22
 */

/**
 * Controller
 */
@Controller
@RequestMapping({"/service"})
public class ServiceController {
	private static String PAGE_FILE_NAME = "service/";

	@Autowired
	private AgentServService agentServService;

	@Autowired
	private OneClassService oneClassService;
	@Autowired
	private DictService dictService;


	@RequestMapping(value = {"/oneclass/index"})
	public String index(@RequestParam(value = "pageNum", required = true, defaultValue = PageConstants.PAGE_NUM+"") int pageNum,
										@RequestParam(value = "pageSize", required = true, defaultValue = PageConstants.PAGE_SIZE+"") int pageSize,
						ModelMap model) {
		PageInfo<OneClass> pageInfo = oneClassService.findAll(pageNum,pageSize);
		model.put("oneClassList",DictUtil.keyValueList("SERVICE_CLASS"));
		model.put("pageInfo", pageInfo);

		return PAGE_FILE_NAME + "index";
	}


	@RequestMapping(value = "/oneclass/add", method = RequestMethod.GET)
	public String add(ModelMap map) {
		OneClass oneClass1 = new OneClass();
		map.put("oneClass", oneClass1);
		map.put("isAdd", true);
		Map<String,String> dictMap =  DictUtil.keyValueList("SERVICE_CLASS");
		//查询分站已经添加的服务
		List<OneClass> oneClassList = oneClassService.queryOneClassAll();
		List<OneClass> noAddList = new ArrayList<>();
		for(String key :dictMap.keySet()){
			if(!"1".equals(key)){
				boolean isExists = false;
				for(OneClass oneClass:oneClassList){
					if(oneClass.getServiceClass().equals(key)){
						isExists = true;
					}

				}
				if(!isExists){
					OneClass oneClass = new OneClass();
					oneClass.setServiceClass(key);
					oneClass.setServiceName(dictMap.get(key));
					noAddList.add(oneClass);
				}


			}

		}
		map.put("serviceList", noAddList);

		return PAGE_FILE_NAME+"form";
	}

	@RequestMapping(value = "/oneclass/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable String id, ModelMap map) {
		Dict dict = dictService.getById(id);
		map.put("service", dict);
		map.put("isAdd", false);
		return PAGE_FILE_NAME+"form";
	}


	@RequestMapping(value = {"/oneclass/edit"}, method = RequestMethod.POST)
	@ResponseBody
	public JsonResult edit(OneClass oneClass, Boolean isAdd,ModelMap map) {
		try {
			if(isAdd){
				//判断该一级分类是否已经添加过
				OneClass oneClass1 = oneClassService.queryOneClassByServiceCode(oneClass.getServiceClass());
				if(oneClass1 ==null){
					// 新建保存
					oneClass.setAgentId(SessionUtil.getAgentId());
					oneClass.setCreateAt(new Date());
					oneClass.setCreateBy(SessionUtil.getUserId());
					oneClass.setStatus(Constant.DeleteStateEnum.NORMAL.getCode());
					oneClassService.save(oneClass);
				}else{
					if(oneClass1.getStatus()==Constant.DeleteStateEnum.NORMAL.getCode()){
						return JsonResult.failure("该分类已经启用！");
					}else{
						return JsonResult.failure("2",oneClass1.getId().toString());
					}
				}
			}else{
				// 编辑保存
				oneClassService.updateById(oneClass);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}
	@RequestMapping(value = "/oneclass/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public JsonResult delete(@PathVariable String id, ModelMap map) {
		try {
			OneClass oneClass = oneClassService.getById(id);
			oneClassService.removeById(id);
			agentServService.removeByOneClass(oneClass.getAgentId(),oneClass.getServiceClass());
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}

	@RequestMapping(value = "/oneclass/stop/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult stop(@PathVariable String id, ModelMap map) {
		try {
			OneClass oneClass = oneClassService.getById(id);
			oneClass.setStatus(Constant.DeleteStateEnum.DELETE.getCode());
			oneClassService.updateById(oneClass);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}
	@RequestMapping(value = "/oneclass/active/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult active(@PathVariable String id, ModelMap map) {
		try {
			OneClass oneClass = oneClassService.getById(id);
			oneClass.setStatus(Constant.DeleteStateEnum.NORMAL.getCode());
			oneClassService.updateById(oneClass);
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}
}
